﻿using System;
using System.Web;

namespace Cms.Domain
{
    public class SearchContentFilter
    {
        private int _categoryID = -1;
        private int _currentPage;
        private string _contentName = string.Empty;
        private string _freetext = string.Empty;
        private int _pageSize = 10;
        private SortDirection _sortDirection = SortDirection.Ascending;
        private ContentSortMode _sortMode = ContentSortMode.SortKey;
        private int _localeID;

        public int Count { get; set; }

        public SearchContentFilter(string query)
        {
            foreach (string part in query.Split("&".ToCharArray()))
            {
                if (part.Length == 0)
                    continue;
                string key = part.Substring(0, part.IndexOf("="));
                string value = part.Substring(part.IndexOf("=") + 1);
                switch (key)
                {
                    case "LocaleID":
                        _localeID = int.Parse(value);
                        break;
                    case "CategoryID":
                        _categoryID = int.Parse(value);
                        break;
                    case "PageSize":
                        _pageSize = int.Parse(value);
                        break;
                    case "Freetext":
                        _freetext = HttpUtility.UrlDecode(value);
                        break;
                    case "ContentName":
                        _contentName = HttpUtility.UrlDecode(value);
                        break;
                    case "SortMode":
                        _sortMode = (ContentSortMode)Enum.Parse(typeof(ContentSortMode), value, true);
                        break;
                    case "SortDirection":
                        _sortDirection = (SortDirection)Enum.Parse(typeof(SortDirection), value, true);
                        break;
                }
            }
        }

        public int CategoryId
        {
            get
            {
                return _categoryID;
            }
            set
            {
                if (value != _categoryID)
                    _currentPage = 0;
                _categoryID = value;
            }
        }

        public int LocaleId
        {
            get
            {
                return _localeID;
            }
            set
            {
                if (value != _localeID)
                    _currentPage = 0;
                _localeID = value;
            }
        }

        public string Freetext
        {
            get
            {
                return _freetext;
            }
            set
            {
                if (value != _freetext)
                    _currentPage = 0;
                _freetext = value;
            }
        }

        public string ContentName
        {
            get
            {
                return _contentName;
            }
            set
            {
                if (value != _contentName)
                    _currentPage = 0;
                _contentName = value;
            }
        }

        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                if (value != _pageSize)
                    _currentPage = (int)Math.Floor(_currentPage * (float)_pageSize / value);
                _pageSize = value;
            }
        }
        
        public int CurrentPage
        {
            get { return _currentPage; }
            set { _currentPage = value; }
        }

        public ContentSortMode SortMode
        {
            get { return _sortMode; }
            set { _sortMode = value; }
        }

        public SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }

        public string GetQuery()
        {
            string query = "CategoryID=" + _categoryID +
                           "&LocaleID=" + _localeID +
                           "&CurrentPage=" + _currentPage +
                           "&PageSize=" + _pageSize +
                           "&Freetext=" + HttpUtility.UrlEncode(_freetext) +
                           "&ContentName=" + HttpUtility.UrlEncode(_contentName) +
                           "&SortMode=" + _sortMode +
                           "&SortDirection=" + _sortDirection;
            return query;
        }
    }
}